package com.example.sample.activity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.example.app.BaseActivity;
import com.example.sample.R;

import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;

import net.liteheaven.xblib.common.java.ReflectUtil;
import net.liteheaven.xblib.widget.edittext.FixedLineCountEditText;

public class BaseStationActivity extends BaseActivity {
	
	private static int MAX_LINE = 8;
	private TelephonyManager mTelephonyManager;
	private FixedLineCountEditText mLogEdit;
	private TextView mTvSignal;
    private TextView mTvNetworkInfo;
	private StringBuffer mLogContent = new StringBuffer();
	private PhoneStateListener mStateListener = new PhoneStateListener(){
		
		public void onSignalStrengthChanged(int asu) {
			//addLog("onSignalStrengthChanged asu = " + asu);
		};
		
		public void onSignalStrengthsChanged(SignalStrength signalStrength) {
			addLog("onSignalStrengthsChanged asuLevel = " + getAsuLevel(signalStrength));
			mTvSignal.setText(signalStrength.toString());
            update();
		};
		
		public void onCallStateChanged(int state, String incomingNumber) {
			addLog("onCallStateChanged state = " + state + " incomingNumber = " + incomingNumber);
		};
		
		public void onCellInfoChanged(java.util.List<android.telephony.CellInfo> cellInfo) {
			addLog("onCellInfoChanged cellInfo = " + cellInfo.toString());
		};
		
		public void onDataConnectionStateChanged(int state) {
			addLog("onDataConnectionStateChanged state = " + state);
		};
		
		public void onDataConnectionStateChanged(int state, int networkType) {
			addLog("onDataConnectionStateChanged state = " + state + " networkType + " + networkType);
		};
		
		public void onServiceStateChanged(android.telephony.ServiceState serviceState) {
			addLog("onServiceStateChanged serviceState = " + serviceState);
		};
		
		public void onCallForwardingIndicatorChanged(boolean cfi) {
			addLog("onCallForwardingIndicatorChanged cfi = " + cfi);
		};
		
		public void onCellLocationChanged(android.telephony.CellLocation location) {
			addLog("onCellLocationChanged location = " + location);
		};
		
		public void onMessageWaitingIndicatorChanged(boolean mwi) {
			addLog("onMessageWaitingIndicatorChanged location = " + mwi);
		};
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_base_station);
		mLogEdit = (FixedLineCountEditText)findViewById(R.id.logEdit);
		mTvSignal = (TextView)findViewById(R.id.tvSignal);
        mTvNetworkInfo = (TextView)findViewById(R.id.tvNetworkInfo);
		listen();
	}
	
	@Override
	protected void onDestroy() {
		mTelephonyManager.listen(mStateListener, 0);
		super.onDestroy();
	}
	
	private void listen(){
		mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		mTelephonyManager.listen(mStateListener, 290);
	}
	
	private void addLog(String content){
		Log.i(TAG, content);
		mLogEdit.appendLine(content);
	}
	
	public static int getAsuLevel(SignalStrength ss){
		int asuLevel = 99;
		try {
			Method getAsuLevel = SignalStrength.class.getMethod("getAsuLevel");
			getAsuLevel.setAccessible(true);
			try {
				asuLevel = (Integer)getAsuLevel.invoke(ss);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			getAsuLevel.setAccessible(false);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return asuLevel;
	}

    private void update(){
        String report = "";
        String line = "";
        String value = "";
        TelephonyManager mgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        final String PHONE_TYPE [] = {"NONE","GSM","CDMA","SIP"};
        final String NETWORK_TYPE [] = {
                "UNKNOWN",
                "GPRS",
                "EDGE",
                "UMTS",
                "CDMA",
                "EVDO_0",
                "EVDO_A",
                "1xRTT",
                "HSDPA",
                "HSUPA",
                "HSPA",
                "IDEN",
                "EVDO_B",
                "LTE",
                "EHRPD",
                "HSPAP"
        };
        final String SIM_STATE [] = {
                "SIM_STATE_UNKNOWN",
                "SIM_STATE_ABSENT",
                "SIM_STATE_PIN_REQUIRED",
                "SIM_STATE_PUK_REQUIRED",
                "SIM_STATE_NETWORK_LOCKED",
                "SIM_STATE_READY"
        };

        int networkType = mgr.getNetworkType();
        String ntTxt = (networkType < NETWORK_TYPE.length ? NETWORK_TYPE[networkType]  : "未知") + "(" + networkType + ")";
        line = "网络类型 [ " + networkType + " ] " + ntTxt + "\n";
        report += line;

        int phoneType = mgr.getPhoneType();
        line = "话机类型 [ " + phoneType + " ] " +PHONE_TYPE[phoneType] + "\n";
        report += line;

        value = mgr.getDeviceSoftwareVersion();
        line = "设备软件版本 " + value + "\n";
        report += line;

        value = mgr.getDeviceId();
        line = "设备号 " + value + "\n";
        report += line;

        value = (String) ReflectUtil.invokeMethod(mgr, "getGroupIdLevel1", null, null);
        if (value == null){
            value = "null";
        }
        line = "LV1组ID " + value + "\n";
        report += line;

        value = mgr.getLine1Number();
        line = "Line1NO " + value + "\n";
        report += line;

        int simState = mgr.getSimState();
        line = "SIM状态 " + SIM_STATE[simState] + "\n";
        report += line;

        value = mgr.getSimCountryIso();
        line = "SIMCountryISO " + value + "\n";
        report += line;

        value = mgr.getSimOperator();
        line = "Sim操作者 " + value + "\n";
        report += line;

        value = mgr.getSimOperatorName();
        line = "SIM操作者名 " + value + "\n";
        report += line;

        value = mgr.getSimSerialNumber();
        line = "SIM序列号 " + value + "\n";
        report += line;

        int dataState = mgr.getDataState();
        line = "数据状态 " + dataState + "\n";
        report += line;

        value = mgr.getSubscriberId();
        line = "签署者ID " + value + "\n";
        report += line;

        mTvNetworkInfo.setText(report);
    }
}
